<template>
  <DaprPlusTable
    :table-title="$t('fsb.softwares.taskPlan.list')"
    :entity="entity"
    :grid-options="gridOptions"
    :query-api="crudApi.getPage"
    :create-api="crudApi.create"
    :update-api="crudApi.update"
    :delete-api="crudApi.delete"
    :default-new-record="createDefaultTaskPlan"
    @update:grid-api="onGridApiUpdate"
  />
</template>

<script setup lang="ts">
import type { ExtendedVxeGridApi, VxeGridProps } from '#/adapter/vxe-table';
import { DaprPlusTable } from '#/components/daprplus';
import { createCrudApi } from '#/api/daprplus';
import type { IInDto, IOutDto, EntityInfo } from '#/utils/daprplus';
import { $t } from '#/locales';

const baseUrl = '/softwares/api/taskplan';
const crudApi = createCrudApi<TaskPlanInDto, TaskPlanOutDto>(baseUrl);

const entity: EntityInfo = {
  code: 'TaskPlan',
  display: $t('fsb.softwares.taskPlan.title'),
};

// 验证规则
const validRules = {
  name: [
    { required: true, message: '任务计划名称不能为空' },
    { min: 2, max: 50, message: '任务计划名称长度应在2-50个字符之间' },
  ],
  functions: [
    { required: true, message: '关联功能不能为空' },
    { max: 500, message: '关联功能长度应在0-500个字符之间' },
  ],
};

// 扩展配置
const gridOptions: VxeGridProps<TaskPlanOutDto> = {
  editRules: validRules,
  columns: [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '编号',
      sortable: true,
      width: 130,
    },
    {
      field: 'name',
      title: '任务计划名称',
      sortable: true,
      align: 'left',
      minWidth: 150,
      editRender: { name: 'input' },
    },
    {
      field: 'functions',
      title: '关联功能',
      align: 'left',
      minWidth: 200,
      editRender: { name: 'input' },
    },
    {
      field: 'userId',
      title: '用户编号',
      align: 'center',
      width: 100,
      editRender: { name: 'input' },
    },
    {
      field: 'createdTime',
      title: '创建时间',
      formatter: 'formatDateTime',
      width: 150,
    },
  ],
};

let gridApi: ExtendedVxeGridApi;

function onGridApiUpdate(api: ExtendedVxeGridApi) {
  gridApi = api;
}

function createDefaultTaskPlan(): TaskPlanInDto {
  return {
    id: '0',
    name: '新任务计划',
    functions: '',
    userId: 0,
  };
}

export interface TaskPlanInDto extends IInDto {
  name: string;
  functions: string;
  userId: number;
}

export interface TaskPlanOutDto extends TaskPlanInDto, IOutDto {
  createdTime: string;
}
</script>

<style scoped></style>
